package com.dcits.annotation;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class ValidateUtil {
    private ValidateUtil() {
    }

    public static String checkField(Field field, Object value, FieldCheck rule) {
        StringBuilder errorBuilder = new StringBuilder();
        // 1. 非空校验
        checkNotNull(value, rule, errorBuilder);

        if (value != null) {
            String strValue = value.toString();
            // 2. 枚举值校验
            checkEnum(strValue, rule, errorBuilder);

            // 3. 长度校验（智能提示）
            checkLength(strValue, rule, errorBuilder);

            // 4. 正则表达式校验
            checkRegex(strValue, rule, errorBuilder);

            checkDigit(strValue, rule, errorBuilder);

        }
        if (errorBuilder.length() == 0) {
            return null;
        }
        return new StringBuilder("字段[" + field.getName() + "] ").append(rule.desc()).append("：").append(errorBuilder).toString();
    }

    private static void checkNotNull(Object value, FieldCheck rule, StringBuilder errorBuilder) {
        if (rule.notNull() && value == null) {
            errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append("不允许为空");
        }
    }

    private static void checkDigit(String value, FieldCheck rule, StringBuilder errorBuilder) {
        if (rule.digit()) {
            String patternStr = "\\d*";
            Pattern pattern = Pattern.compile(patternStr);
            if (StringUtils.isNotEmpty(value) && !pattern.matcher(value).matches()) {
                errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append("必须是数字");
            }
        }
    }

    private static void checkEnum(String strValue, FieldCheck rule, StringBuilder errorBuilder) {
        // 2. 枚举值校验
        if (!rule.in().isEmpty()) {
            List<String> allowedValues = Arrays.asList(rule.in().split(","));
            if (!allowedValues.contains(StringUtils.trim(strValue))) {
                String errorMsg = String.format("只允许[%s]中的值", rule.in());
                errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append(errorMsg);
            }
        }
    }

    private static void checkLength(String strValue, FieldCheck rule, StringBuilder errorBuilder) {
        int length = strValue.length();
        if (rule.minLength() > 0 && rule.maxLength() > 0) {
            checkLengthMinMax(strValue, rule, errorBuilder);
        } else if (rule.minLength() > 0 && length < rule.minLength()) {
            errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append("长度不能小于").append(rule.minLength());
        } else if (rule.maxLength() > 0 && length > rule.maxLength()) {
            errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append("长度不能大于").append(rule.minLength());
        }
    }

    private static void checkLengthMinMax(String strValue, FieldCheck rule, StringBuilder errorBuilder) {
        int length = strValue.length();
        if (length < rule.minLength() || length > rule.maxLength()) {
            String errorMsg = String.format("长度需在%d~%d之间", rule.minLength(), rule.maxLength());
            errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append(errorMsg);
        }
    }

    private static void checkRegex(String strValue, FieldCheck rule, StringBuilder errorBuilder) {
        if (!rule.regex().isEmpty() && !strValue.matches(rule.regex())) {
            errorBuilder.append(errorBuilder.length() > 0 ? "," : "").append("格式不符合要求");
        }
    }
}